﻿using System;
using System.Data;
using System.Text;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;

namespace Chenxin.Lib.Business.DAL
{
    /// <summary>
    /// 数据访问类t_Preliminary_SubInfo1。
    /// </summary>
    public class t_Preliminary_SubInfo1
    {
        public t_Preliminary_SubInfo1()
        { }
        #region  成员方法
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public static bool Exists(Guid RecordId)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from t_Preliminary_SubInfo1 where RecordId=@RecordId ");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            int cmdresult;
            object obj = db.ExecuteScalar(dbCommand);
            if ((Object.Equals(obj, null)) || (Object.Equals(obj, System.DBNull.Value)))
            {
                cmdresult = 0;
            }
            else
            {
                cmdresult = int.Parse(obj.ToString());
            }
            if (cmdresult == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public static void Add(Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1 model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into t_Preliminary_SubInfo1(");
            strSql.Append("RecordId,MainId,CustomerName,IDNum,GJJNum,SpousesName,SpousesIDNum,SpousesGJJNum,BOwner)");

            strSql.Append(" values (");
            strSql.Append("@RecordId,@MainId,@CustomerName,@IDNum,@GJJNum,@SpousesName,@SpousesIDNum,@SpousesGJJNum,@BOwner)");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "MainId", DbType.Guid, model.MainId);
            db.AddInParameter(dbCommand, "CustomerName", DbType.AnsiString, model.CustomerName);
            db.AddInParameter(dbCommand, "IDNum", DbType.AnsiString, model.IDNum);
            db.AddInParameter(dbCommand, "GJJNum", DbType.AnsiString, model.GJJNum);
            db.AddInParameter(dbCommand, "SpousesName", DbType.AnsiString, model.SpousesName);
            db.AddInParameter(dbCommand, "SpousesIDNum", DbType.AnsiString, model.SpousesIDNum);
            db.AddInParameter(dbCommand, "SpousesGJJNum", DbType.AnsiString, model.SpousesGJJNum);
            db.AddInParameter(dbCommand, "BOwner", DbType.Boolean, model.BOwner);
            db.ExecuteNonQuery(dbCommand);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public static void Update(Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1 model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update t_Preliminary_SubInfo1 set ");
			strSql.Append("MainId=@MainId,");
			strSql.Append("CustomerName=@CustomerName,");
			strSql.Append("IDNum=@IDNum,");
			strSql.Append("GJJNum=@GJJNum,");
			strSql.Append("SpousesName=@SpousesName,");
			strSql.Append("SpousesIDNum=@SpousesIDNum,");
			strSql.Append("SpousesGJJNum=@SpousesGJJNum,");
			strSql.Append("BOwner=@BOwner");
			strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
			DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
			db.AddInParameter(dbCommand, "RecordId", DbType.Guid, model.RecordId);
            db.AddInParameter(dbCommand, "MainId", DbType.Guid, model.MainId);
			db.AddInParameter(dbCommand, "CustomerName", DbType.AnsiString, model.CustomerName);
			db.AddInParameter(dbCommand, "IDNum", DbType.AnsiString, model.IDNum);
			db.AddInParameter(dbCommand, "GJJNum", DbType.AnsiString, model.GJJNum);
			db.AddInParameter(dbCommand, "SpousesName", DbType.AnsiString, model.SpousesName);
			db.AddInParameter(dbCommand, "SpousesIDNum", DbType.AnsiString, model.SpousesIDNum);
			db.AddInParameter(dbCommand, "SpousesGJJNum", DbType.AnsiString, model.SpousesGJJNum);
			db.AddInParameter(dbCommand, "BOwner", DbType.Boolean, model.BOwner);
			db.ExecuteNonQuery(dbCommand);

		}

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public static void Delete(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_Preliminary_SubInfo1 ");
            strSql.Append(" where RecordId=@RecordId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "RecordId", DbType.Guid, RecordId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 删除某一预审关联数据
        /// </summary>
        public static void DeleteByMainId(Guid MainId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from t_Preliminary_SubInfo1 ");
            strSql.Append(" where MainId=@MainId ");
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            db.AddInParameter(dbCommand, "MainId", DbType.Guid, MainId);
            db.ExecuteNonQuery(dbCommand);

        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1 GetModel(Guid RecordId)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select RecordId,MainId,CustomerName,IDNum,GJJNum,SpousesName,SpousesIDNum,SpousesGJJNum,BOwner from t_Preliminary_SubInfo1 ");
			strSql.Append(" where RecordId=@RecordId ");
			Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
			DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
			db.AddInParameter(dbCommand, "RecordId", DbType.Guid,RecordId);
            Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1 model = null;
			using (IDataReader dataReader = db.ExecuteReader(dbCommand))
			{
				if(dataReader.Read())
				{
					model=ReaderBind(dataReader);
				}
				dataReader.Close();
                dataReader.Dispose();
			}
			return model;
		}

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public static Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1 GetMainModel(Guid RecordId)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,MainId,CustomerName,IDNum,GJJNum,SpousesName,SpousesIDNum,SpousesGJJNum,BOwner from t_Preliminary_SubInfo1 ");
            strSql.AppendFormat(" where BOwner = 1 and MainId = '{0}' ", RecordId);
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            DbCommand dbCommand = db.GetSqlStringCommand(strSql.ToString());
            Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1 model = null;
            using (IDataReader dataReader = db.ExecuteReader(dbCommand))
            {
                if (dataReader.Read())
                {
                    model = ReaderBind(dataReader);
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public static DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,MainId,CustomerName,IDNum,GJJNum,SpousesName,SpousesIDNum,SpousesGJJNum,BOwner ");
            strSql.Append(" FROM t_Preliminary_SubInfo1 ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 根据HouseId获取最新上家信息
        /// </summary>
        public static DataSet GetLatestListByHouseId(string HouseId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("SELECT RecordId,MainId,CustomerName,IDNum,GJJNum,SpousesName,SpousesIDNum,SpousesGJJNum,BOwner ");
            strSql.Append("FROM t_Preliminary_SubInfo1 ");
            strSql.Append("WHERE EXISTS(");
            strSql.Append("             SELECT 1 ");
            strSql.Append("             FROM t_Preliminary_BaseInfo ");
            strSql.Append("             WHERE Mainid = RecordId ");
            strSql.Append("             AND LastDate = ( ");
            strSql.Append("                 SELECT MAX(LastDate) ");
            strSql.Append("                 FROM t_Preliminary_BaseInfo ");
            strSql.AppendFormat("           WHERE HouseId = '{0}' ", HouseId);
            strSql.Append("                 ) ");
            strSql.Append("             ) ");
            strSql.Append("ORDER BY BOwner ");
            List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1> list = new List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 获得数据列表（比DataSet效率高，推荐使用）
        /// </summary>
        public static List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1> GetListArray(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select RecordId,MainId,CustomerName,IDNum,GJJNum,SpousesName,SpousesIDNum,SpousesGJJNum,BOwner ");
            strSql.Append(" FROM t_Preliminary_SubInfo1 ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1> list = new List<Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1>();
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            using (IDataReader dataReader = db.ExecuteReader(CommandType.Text, strSql.ToString()))
            {
                while (dataReader.Read())
                {
                    list.Add(ReaderBind(dataReader));
                }
                dataReader.Close();
                dataReader.Dispose();
            }
            return list;
        }


        /// <summary>
        /// 对象实体绑定数据
        /// </summary>
        public static Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1 ReaderBind(IDataReader dataReader)
        {
            Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1 model = new Chenxin.Lib.Business.Entity.t_Preliminary_SubInfo1();
            object ojb;
            ojb = dataReader["RecordId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.RecordId = new Guid(ojb.ToString());
            }
            ojb = dataReader["MainId"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.MainId = new Guid(ojb.ToString());
            }
            model.CustomerName = dataReader["CustomerName"].ToString();
            model.IDNum = dataReader["IDNum"].ToString();
            model.GJJNum = dataReader["GJJNum"].ToString();
            model.SpousesName = dataReader["SpousesName"].ToString();
            model.SpousesIDNum = dataReader["SpousesIDNum"].ToString();
            model.SpousesGJJNum = dataReader["SpousesGJJNum"].ToString();
            ojb = dataReader["BOwner"];
            if (ojb != null && ojb != DBNull.Value)
            {
                model.BOwner = (bool)ojb;
            }
            return model;
        }

        /// <summary>
        /// 通过RecordId得到一个对象实体
        /// </summary>
        public static DataSet GetListArray(Guid RecordId)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" select b.CustomerName,b.IDNum,b.GJJNum,b.SpousesName,b.SpousesIDNum,b.SpousesGJJNum,b.BOwner ");
            strSql.Append(" from t_Preliminary_BaseInfo a left join t_Preliminary_SubInfo1 b on a.RecordId = b.MainId ");
            strSql.AppendFormat(" where a.RecordId='{0}' and b.BOwner = 1", RecordId);
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            return db.ExecuteDataSet(CommandType.Text, strSql.ToString());
        }

        /// <summary>
        /// 分页获取数据列表
        /// </summary>
        public static DataSet GetList(int PageIndex, int PageSize, String OrderField, string TableName, string StrWhere, string FieldList, string TableFieldList, bool DoCount)
        {
            Database db = DatabaseFactory.CreateDatabase("Chenxin_Business_Search");
            DbCommand dbCommand = db.GetStoredProcCommand("PagerMultiTable");
            db.AddInParameter(dbCommand, "PageIndex", DbType.Int32, PageIndex);
            db.AddInParameter(dbCommand, "PageSize", DbType.Int32, PageSize);
            db.AddInParameter(dbCommand, "OrderField", DbType.AnsiString, OrderField);
            db.AddInParameter(dbCommand, "TableName", DbType.AnsiString, TableName);
            db.AddInParameter(dbCommand, "StrWhere", DbType.AnsiString, StrWhere);
            db.AddInParameter(dbCommand, "FieldList", DbType.AnsiString, FieldList);
            db.AddInParameter(dbCommand, "TableFieldList", DbType.AnsiString, TableFieldList);
            db.AddInParameter(dbCommand, "DoCount", DbType.Boolean, DoCount);
            return db.ExecuteDataSet(dbCommand);
        }
        #endregion  成员方法
    }
}